package sctu.edu.java.year2016.team07.project.admin;

import sctu.edu.java.year2016.team08.yangying.day20160527.chapter7_1.S;

import java.io.*;
import java.util.Date;

/**
 * Created by Huang Suixin on 2016/6/30.
 */
public class Server {

    private int recordNum;
    //获得反馈文件夹中的反馈数目
    protected int getRecordNum(){
        File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\record");
        String[] filelist = dir.list();
        return filelist.length;
    }

    //获得反馈文件夹中指定序列号的文件的名字
    protected String getRecordName(int i){
        File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\record");
        String[] filelist = dir.list();
        return filelist[i];
    }

    //获得指定文件名的内容
    protected String getContent(String name){
        File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\record");
        File file = new File(dir , name);

        char[] getStr = new char[4800];     //用来存放从文件读取的内容；
        if (name.equals("")||!file.exists()) {
            return "该问题已解决！";
        } else {
            FileReader fr = null;
            try {
                fr = new FileReader(file);
                fr.read(getStr);        //将文件中的内容读出来，存到getStr字符数组中
                fr.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new String(getStr);
    }

    //删除指定文件名的文件
    protected void deleteFile(String name){
        if (name.equalsIgnoreCase("")){
            return;
        }else{
            File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\record");
            File file = new File(dir,name);
            file.delete();
        }
    }

    //向问题库中增加问题
    protected void addAnswer(String name,String content){
        File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\question");
        File file = new File(dir,name);

        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        System.out.println(server.getRecordNum());
        System.out.println(server.getRecordName(1));
        System.out.println(server.getContent(server.getRecordName(0)));

        server.addAnswer("玩游戏卡","那几不玩游戏");
    }

}
